/**
 * Heterotopia - an up-to-date Java-based MUD system
 * Copyright (C) 2011 Bill Garrett <memesis@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package heterotopia.oa;

import java.util.HashMap;
import java.util.Map;

import org.apache.camel.Processor;

/**
 * A helper class that constructs script-backed objects based on a type
 * (a string label, such as "Player" or "Room").
 * 
 * @author garrett
 */
public class TypeFactoryMap {
	private Map<String, TypeFactory> typeRegistry;
	
	public TypeFactoryMap() {
		typeRegistry = new HashMap<String, TypeFactory>();
	}
	
	/**
	 * Return the map of types to type factories currently registered.
	 * 
	 * @return a map of type names and factories
	 */
	public Map<String, TypeFactory> getTypeRegistry() {
		return typeRegistry;
	}

	/**
	 * Registers a named type and a type factory for that type.
	 * 
	 * @param type the type to register (e.g. "Player", "Room")
	 * @param factory an implementation of TypeFactory capable of producing the type in question
	 */
	public void register(String type, TypeFactory factory) {
		typeRegistry.put(type, factory);
	}
	
	/**
	 * Given a named type, attempt to construct an instance of that type.
	 * TODO: include a persistence partner.
	 * 
	 * @param type the type to create (e.g. "Player", "Room")
	 * @return a new instance of that object, or null
	 */
	public Processor create(String type) {
		TypeFactory factory = typeRegistry.get(type);
		if(factory == null)
			return null;
		return factory.create(type);
	}
}